/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.yunsc.common.utils;


import cn.hutool.http.HttpStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 响应数据
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
@ApiModel(value = "响应")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 编码：0表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：200表示成功，其他值表示失败")
    @JsonProperty("status")
    private int code = 200;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String message = "success";
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;


    public static<T> Result<T> ok() {
        return new Result<>();
    }

    public static<T> Result<T> ok(String msg) {
        Result<T> r = new Result<>();
        r.setMessage(msg);
        return r;
    }

    public static<T> Result<T> ok(T data) {
        Result<T> r = new Result<T>();
        r.setData(data);
        return r;
    }
    public static<T> Result<T> ok(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setMessage(msg);
        r.setData(data);
        return r;
    }
    public static<T> Result<T> ok(int code, String msg) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }
    public static<T> Result<T> ok(int code, String msg, T data) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }
    public static Result<Map<String, Object>> resultList(List<?> list) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return Result.ok(map);
    }
    public static Result<Map<String, Object>> resultPageList(PageResult<?> page) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", page);
        return Result.ok(map);
    }

    public static<T> Result<T> error() {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, "出错啦，请联系管理员");
    }

    public static<T> Result<T> error(int code) {
        return error(code, "出错啦，请联系管理员");
    }

    public static<T> Result<T> error(int code, String msg) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }

    public static<T> Result<T> error(String msg) {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, msg);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String msg) {
        this.message = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
